package com.obrempon.credit.market.ui.dialog

import android.content.Context
import android.graphics.Paint
import android.view.LayoutInflater
import android.view.View
import com.obrempon.credit.market.R
import com.obrempon.credit.market.common.CommonDialog
import com.obrempon.credit.market.databinding.ObremponScoreStarNormalDialogBinding
import com.obrempon.credit.market.event.ObremponEventPointType
import com.obrempon.credit.market.utils.helper.ObremponScoreDialogHelper

/**
 * ObremponCredit
 * @Author wwdeng
 * @CreateTime: 2024/8/20 16:26
 * @Annotation：评分 Normal
 */
class ObremponScoreDialog: CommonDialog {
    private lateinit var binding: ObremponScoreStarNormalDialogBinding
    private var mCallback: ObremponScoreDialogHelper.IStarDialogCallback? = null

    constructor(context: Context) : super(context) {}
    constructor(context: Context, callback: ObremponScoreDialogHelper.IStarDialogCallback?) : super(context) {
        mCallback = callback
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: ObremponScoreDialogHelper.IStarDialogCallback){
        this.mCallback = callback
    }

    override fun takeObremponRootView(): View {
        binding = ObremponScoreStarNormalDialogBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initObremponView() {
        binding.buttonScoreNotNow.paint.flags = Paint.UNDERLINE_TEXT_FLAG//下划线
        binding.buttonScoreNotNow.paint.isAntiAlias = true//抗锯齿
    }

    override fun initObremponListener() {
        setOnClickListener(binding.imageScoreClose) //关闭
        setOnClickListener(binding.buttonScoreNotNow)
        setOnClickListener(binding.buttonScoreRate)

        binding.scoreRatingBar.setIsIndicator(false)
        binding.scoreRatingBar.setOnRatingChangeListener { ratingBar, rating, fromUser ->
            addEventPointRequestAction(ObremponEventPointType.CLICK.OBREMPON_CLICK_SCORE_STAR,rating.toInt().toString())
        }
    }

    override fun takeObremponData() {
    }

    override fun onObremponViewClick(v: View?) {
        when(v!!.id){
            R.id.imageScoreClose,
            R.id.buttonScoreNotNow -> {//关闭、取消
                addEventPointRequestAction(ObremponEventPointType.CLICK.OBREMPON_CLICK_SCORE_NOT_NOW,"")
                dismiss()
            }
            else -> {//确定
                val count = binding.scoreRatingBar.rating.toInt()
                if (count <= 0){
                    return
                }
                if(mCallback != null){
                    mCallback!!.completeScoringCount(count)
                }
                dismiss()
            }
        }
    }

    fun showObremponScoreTipsText(tips: String){
        binding.textScoreTips.text = tips
    }

    override fun onStart() {
        super.onStart()
        addEventPointRequestAction(ObremponEventPointType.DIALOG.OBREMPON_DIALOG_USER_RATE_US_SHOW)
    }

    override fun dismiss() {
        super.dismiss()
        addEventPointRequestAction(ObremponEventPointType.DIALOG.OBREMPON_DIALOG_USER_RATE_US_DISMISS)
    }
}